{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "25f98032-6bc5-4530-bcb4-26b0218a366b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>behavior_type</th>\n",
       "      <th>user_geohash</th>\n",
       "      <th>item_category</th>\n",
       "      <th>time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>98047837</td>\n",
       "      <td>232431562</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4245</td>\n",
       "      <td>2014-12-06 02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>97726136</td>\n",
       "      <td>383583590</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5894</td>\n",
       "      <td>2014-12-09 20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>98607707</td>\n",
       "      <td>64749712</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2883</td>\n",
       "      <td>2014-12-18 11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>98662432</td>\n",
       "      <td>320593836</td>\n",
       "      <td>1</td>\n",
       "      <td>96nn52n</td>\n",
       "      <td>6562</td>\n",
       "      <td>2014-12-06 10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>98145908</td>\n",
       "      <td>290208520</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>13926</td>\n",
       "      <td>2014-12-16 21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    user_id    item_id  behavior_type user_geohash  item_category  \\\n",
       "0  98047837  232431562              1          NaN           4245   \n",
       "1  97726136  383583590              1          NaN           5894   \n",
       "2  98607707   64749712              1          NaN           2883   \n",
       "3  98662432  320593836              1      96nn52n           6562   \n",
       "4  98145908  290208520              1          NaN          13926   \n",
       "\n",
       "            time  \n",
       "0  2014-12-06 02  \n",
       "1  2014-12-09 20  \n",
       "2  2014-12-18 11  \n",
       "3  2014-12-06 10  \n",
       "4  2014-12-16 21  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入分析过程中所使用到的包\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import datetime\n",
    "#导入数据    \n",
    "taobaoData=pd.read_csv(r\"C:\\Users\\职云昊\\OneDrive\\Desktop\\tianchi_mobile_recommend_train_user.csv\")\n",
    "# 查看数据\n",
    "taobaoData.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c8130930-ab88-43f0-9405-44fa399b7061",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>用户ID</th>\n",
       "      <th>商品ID</th>\n",
       "      <th>行为类型</th>\n",
       "      <th>用户地址</th>\n",
       "      <th>商品类别</th>\n",
       "      <th>登录时间</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>98047837</td>\n",
       "      <td>232431562</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4245</td>\n",
       "      <td>2014-12-06 02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>97726136</td>\n",
       "      <td>383583590</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5894</td>\n",
       "      <td>2014-12-09 20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>98607707</td>\n",
       "      <td>64749712</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2883</td>\n",
       "      <td>2014-12-18 11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>98662432</td>\n",
       "      <td>320593836</td>\n",
       "      <td>1</td>\n",
       "      <td>96nn52n</td>\n",
       "      <td>6562</td>\n",
       "      <td>2014-12-06 10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>98145908</td>\n",
       "      <td>290208520</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>13926</td>\n",
       "      <td>2014-12-16 21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>93784494</td>\n",
       "      <td>337869048</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3979</td>\n",
       "      <td>2014-12-03 20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>94832743</td>\n",
       "      <td>105749725</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9559</td>\n",
       "      <td>2014-12-13 20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>95290487</td>\n",
       "      <td>76866650</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10875</td>\n",
       "      <td>2014-11-27 16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>96610296</td>\n",
       "      <td>161166643</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3064</td>\n",
       "      <td>2014-12-11 23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>100684618</td>\n",
       "      <td>21751142</td>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2158</td>\n",
       "      <td>2014-12-05 23</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        用户ID       商品ID  行为类型     用户地址   商品类别           登录时间\n",
       "0   98047837  232431562     1      NaN   4245  2014-12-06 02\n",
       "1   97726136  383583590     1      NaN   5894  2014-12-09 20\n",
       "2   98607707   64749712     1      NaN   2883  2014-12-18 11\n",
       "3   98662432  320593836     1  96nn52n   6562  2014-12-06 10\n",
       "4   98145908  290208520     1      NaN  13926  2014-12-16 21\n",
       "5   93784494  337869048     1      NaN   3979  2014-12-03 20\n",
       "6   94832743  105749725     1      NaN   9559  2014-12-13 20\n",
       "7   95290487   76866650     1      NaN  10875  2014-11-27 16\n",
       "8   96610296  161166643     1      NaN   3064  2014-12-11 23\n",
       "9  100684618   21751142     3      NaN   2158  2014-12-05 23"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改列名\n",
    "colNameDict={'user_id':'用户ID','item_id':'商品ID','behavior_type':'行为类型','user_geohash':'用户地址','item_category':'商品类别','time':'登录时间'}\n",
    "taobaoData.rename(columns=colNameDict,inplace=True)\n",
    "# 查看数据集\n",
    "taobaoData.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0ce3b087-73a5-406b-aa93-91d300b22801",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "用户ID          0\n",
       "商品ID          0\n",
       "行为类型          0\n",
       "用户地址    8334824\n",
       "商品类别          0\n",
       "登录时间          0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看数据集各列的缺失值个数\n",
    "taobaoData.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dc5afbc6-777c-4f1c-9565-e91eba3d212b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12256906, 6)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看数据集的行列数\n",
    "taobaoData.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "eccbcf7b-3344-4220-bf58-c7ba467f1318",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>用户ID</th>\n",
       "      <th>商品ID</th>\n",
       "      <th>行为类型</th>\n",
       "      <th>商品类别</th>\n",
       "      <th>登录时间</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>98047837</td>\n",
       "      <td>232431562</td>\n",
       "      <td>1</td>\n",
       "      <td>4245</td>\n",
       "      <td>2014-12-06 02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>97726136</td>\n",
       "      <td>383583590</td>\n",
       "      <td>1</td>\n",
       "      <td>5894</td>\n",
       "      <td>2014-12-09 20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>98607707</td>\n",
       "      <td>64749712</td>\n",
       "      <td>1</td>\n",
       "      <td>2883</td>\n",
       "      <td>2014-12-18 11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>98662432</td>\n",
       "      <td>320593836</td>\n",
       "      <td>1</td>\n",
       "      <td>6562</td>\n",
       "      <td>2014-12-06 10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>98145908</td>\n",
       "      <td>290208520</td>\n",
       "      <td>1</td>\n",
       "      <td>13926</td>\n",
       "      <td>2014-12-16 21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       用户ID       商品ID  行为类型   商品类别           登录时间\n",
       "0  98047837  232431562     1   4245  2014-12-06 02\n",
       "1  97726136  383583590     1   5894  2014-12-09 20\n",
       "2  98607707   64749712     1   2883  2014-12-18 11\n",
       "3  98662432  320593836     1   6562  2014-12-06 10\n",
       "4  98145908  290208520     1  13926  2014-12-16 21"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除数据框的某一列\n",
    "taobaoData.drop('用户地址',axis=1,inplace=True)\n",
    "#查看数据集\n",
    "taobaoData.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "60f3873d-22c7-435b-828c-57b319013373",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 12256906 entries, 0 to 12256905\n",
      "Data columns (total 5 columns):\n",
      " #   Column  Dtype \n",
      "---  ------  ----- \n",
      " 0   用户ID    int64 \n",
      " 1   商品ID    int64 \n",
      " 2   行为类型    int64 \n",
      " 3   商品类别    int64 \n",
      " 4   登录时间    object\n",
      "dtypes: int64(4), object(1)\n",
      "memory usage: 467.6+ MB\n"
     ]
    }
   ],
   "source": [
    "#查看数据集的信息\n",
    "taobaoData.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "881dd854-b7f0-4b2c-b87b-4a0ba8a5cfed",
   "metadata": {},
   "outputs": [],
   "source": [
    "#修改数据类型\n",
    "taobaoData['用户ID']=taobaoData['用户ID'].astype('object')\n",
    "taobaoData['商品ID']=taobaoData['商品ID'].astype('object')\n",
    "taobaoData['商品类别']=taobaoData['商品类别'].astype('object')\n",
    "taobaoData['行为类型']=taobaoData['行为类型'].astype('object')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bb0441d8-492d-441f-ad5a-da4eb219a97a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 12256906 entries, 0 to 12256905\n",
      "Data columns (total 5 columns):\n",
      " #   Column  Dtype \n",
      "---  ------  ----- \n",
      " 0   用户ID    object\n",
      " 1   商品ID    object\n",
      " 2   行为类型    object\n",
      " 3   商品类别    object\n",
      " 4   登录时间    object\n",
      "dtypes: object(5)\n",
      "memory usage: 467.6+ MB\n"
     ]
    }
   ],
   "source": [
    "taobaoData.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "da9a8fbe-6b3a-48b3-bde4-f7d62c251c98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>用户ID</th>\n",
       "      <th>商品ID</th>\n",
       "      <th>行为类型</th>\n",
       "      <th>商品类别</th>\n",
       "      <th>登录时间</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>98047837</td>\n",
       "      <td>232431562</td>\n",
       "      <td>1</td>\n",
       "      <td>4245</td>\n",
       "      <td>2014-12-06 02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>97726136</td>\n",
       "      <td>383583590</td>\n",
       "      <td>1</td>\n",
       "      <td>5894</td>\n",
       "      <td>2014-12-09 20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>98607707</td>\n",
       "      <td>64749712</td>\n",
       "      <td>1</td>\n",
       "      <td>2883</td>\n",
       "      <td>2014-12-18 11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>98662432</td>\n",
       "      <td>320593836</td>\n",
       "      <td>1</td>\n",
       "      <td>6562</td>\n",
       "      <td>2014-12-06 10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>98145908</td>\n",
       "      <td>290208520</td>\n",
       "      <td>1</td>\n",
       "      <td>13926</td>\n",
       "      <td>2014-12-16 21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12256885</th>\n",
       "      <td>91530370</td>\n",
       "      <td>384717078</td>\n",
       "      <td>2</td>\n",
       "      <td>7876</td>\n",
       "      <td>2014-12-02 23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12256886</th>\n",
       "      <td>91530370</td>\n",
       "      <td>293543750</td>\n",
       "      <td>3</td>\n",
       "      <td>552</td>\n",
       "      <td>2014-12-04 23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12256900</th>\n",
       "      <td>91530370</td>\n",
       "      <td>101985395</td>\n",
       "      <td>3</td>\n",
       "      <td>12090</td>\n",
       "      <td>2014-12-12 21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12256903</th>\n",
       "      <td>93812622</td>\n",
       "      <td>234391443</td>\n",
       "      <td>1</td>\n",
       "      <td>8765</td>\n",
       "      <td>2014-12-11 16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12256904</th>\n",
       "      <td>93812622</td>\n",
       "      <td>26452000</td>\n",
       "      <td>1</td>\n",
       "      <td>7951</td>\n",
       "      <td>2014-12-08 22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6213379 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              用户ID       商品ID 行为类型   商品类别           登录时间\n",
       "0         98047837  232431562    1   4245  2014-12-06 02\n",
       "1         97726136  383583590    1   5894  2014-12-09 20\n",
       "2         98607707   64749712    1   2883  2014-12-18 11\n",
       "3         98662432  320593836    1   6562  2014-12-06 10\n",
       "4         98145908  290208520    1  13926  2014-12-16 21\n",
       "...            ...        ...  ...    ...            ...\n",
       "12256885  91530370  384717078    2   7876  2014-12-02 23\n",
       "12256886  91530370  293543750    3    552  2014-12-04 23\n",
       "12256900  91530370  101985395    3  12090  2014-12-12 21\n",
       "12256903  93812622  234391443    1   8765  2014-12-11 16\n",
       "12256904  93812622   26452000    1   7951  2014-12-08 22\n",
       "\n",
       "[6213379 rows x 5 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#删除重复行\n",
    "taobaoData.drop_duplicates(subset=['用户ID','商品ID','行为类型','商品类别','登录时间'],keep='first')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7a50a207-b2e9-4012-8f34-9bff60527e51",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "************有空值的列*************\n",
      " 用户ID    False\n",
      "商品ID    False\n",
      "行为类型    False\n",
      "商品类别    False\n",
      "登录时间    False\n",
      "dtype: bool\n"
     ]
    }
   ],
   "source": [
    "#  判断空值   axis=0代表为按列算 只要该列有为空或者NA的元素，就为True，否则False\n",
    "print('{:*^30}\\n'.format('有空值的列'),taobaoData.isnull().any(axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "74e74527-5fbf-4dfa-ac21-953cbadba21d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\职云昊\\AppData\\Local\\Temp\\ipykernel_23184\\1195135019.py:27: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  taobaoData1.loc[:,'登录时刻']=dateSer1\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>用户ID</th>\n",
       "      <th>商品ID</th>\n",
       "      <th>行为类型</th>\n",
       "      <th>商品类别</th>\n",
       "      <th>登录时间</th>\n",
       "      <th>登录时刻</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>98047837</td>\n",
       "      <td>232431562</td>\n",
       "      <td>1</td>\n",
       "      <td>4245</td>\n",
       "      <td>2014-12-06</td>\n",
       "      <td>02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>97726136</td>\n",
       "      <td>383583590</td>\n",
       "      <td>1</td>\n",
       "      <td>5894</td>\n",
       "      <td>2014-12-09</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>98607707</td>\n",
       "      <td>64749712</td>\n",
       "      <td>1</td>\n",
       "      <td>2883</td>\n",
       "      <td>2014-12-18</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>98662432</td>\n",
       "      <td>320593836</td>\n",
       "      <td>1</td>\n",
       "      <td>6562</td>\n",
       "      <td>2014-12-06</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>98145908</td>\n",
       "      <td>290208520</td>\n",
       "      <td>1</td>\n",
       "      <td>13926</td>\n",
       "      <td>2014-12-16</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99996</th>\n",
       "      <td>38839327</td>\n",
       "      <td>142636078</td>\n",
       "      <td>1</td>\n",
       "      <td>5503</td>\n",
       "      <td>2014-12-01</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99997</th>\n",
       "      <td>86239261</td>\n",
       "      <td>389384457</td>\n",
       "      <td>1</td>\n",
       "      <td>3662</td>\n",
       "      <td>2014-12-06</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99998</th>\n",
       "      <td>57301300</td>\n",
       "      <td>255239748</td>\n",
       "      <td>1</td>\n",
       "      <td>11770</td>\n",
       "      <td>2014-12-10</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99999</th>\n",
       "      <td>2935703</td>\n",
       "      <td>329752390</td>\n",
       "      <td>1</td>\n",
       "      <td>3673</td>\n",
       "      <td>2014-11-22</td>\n",
       "      <td>06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>100000</th>\n",
       "      <td>11083802</td>\n",
       "      <td>339361946</td>\n",
       "      <td>1</td>\n",
       "      <td>10761</td>\n",
       "      <td>2014-11-25</td>\n",
       "      <td>00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100001 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            用户ID       商品ID 行为类型   商品类别        登录时间 登录时刻\n",
       "0       98047837  232431562    1   4245  2014-12-06   02\n",
       "1       97726136  383583590    1   5894  2014-12-09   20\n",
       "2       98607707   64749712    1   2883  2014-12-18   11\n",
       "3       98662432  320593836    1   6562  2014-12-06   10\n",
       "4       98145908  290208520    1  13926  2014-12-16   21\n",
       "...          ...        ...  ...    ...         ...  ...\n",
       "99996   38839327  142636078    1   5503  2014-12-01   14\n",
       "99997   86239261  389384457    1   3662  2014-12-06   18\n",
       "99998   57301300  255239748    1  11770  2014-12-10   18\n",
       "99999    2935703  329752390    1   3673  2014-11-22   06\n",
       "100000  11083802  339361946    1  10761  2014-11-25   00\n",
       "\n",
       "[100001 rows x 6 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#定义分列函数，获取日期值\n",
    "def splitTime(timeColSer):\n",
    "    timeList=[]\n",
    "    for value in timeColSer:\n",
    "        dateStr1=value.split(' ')[0]\n",
    "        timeList.append(dateStr1)\n",
    "        timeSer=pd.Series(timeList)\n",
    "    return timeSer\n",
    "#定义分列函数，获取时刻值\n",
    "def splitTime1(timeColSer):\n",
    "    timeList=[]\n",
    "    for value in timeColSer:\n",
    "        dateStr1=value.split(' ')[1]\n",
    "        timeList.append(dateStr1)\n",
    "        timeSer=pd.Series(timeList)\n",
    "    return timeSer\n",
    "#在进行分列之前，由于电脑性能的问题，无法处理成百万的数据，需要对数据集进行处理，选取了100000行数据进行分析\n",
    "taobaoData1=taobaoData.loc[0:100000,:]\n",
    "taobaoData2=taobaoData.loc[0:100000,:]\n",
    "#进行分列\n",
    "timeColSer=taobaoData1.loc[:,'登录时间']\n",
    "timeColSer1=taobaoData2.loc[:,'登录时间']\n",
    "dateSer=splitTime(timeColSer)\n",
    "dateSer1=splitTime1(timeColSer1)\n",
    "#赋值给数据框中的列\n",
    "taobaoData1.loc[:,'登录时间']=dateSer\n",
    "taobaoData1.loc[:,'登录时刻']=dateSer1\n",
    "#查看数据集\n",
    "taobaoData1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "caa67406-27f9-4165-8610-194fa35dd969",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>用户ID</th>\n",
       "      <th>商品ID</th>\n",
       "      <th>行为类型</th>\n",
       "      <th>商品类别</th>\n",
       "      <th>登录时间</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>98047837</td>\n",
       "      <td>232431562</td>\n",
       "      <td>pv</td>\n",
       "      <td>4245</td>\n",
       "      <td>2014-12-06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>97726136</td>\n",
       "      <td>383583590</td>\n",
       "      <td>pv</td>\n",
       "      <td>5894</td>\n",
       "      <td>2014-12-09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>98607707</td>\n",
       "      <td>64749712</td>\n",
       "      <td>pv</td>\n",
       "      <td>2883</td>\n",
       "      <td>2014-12-18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>98662432</td>\n",
       "      <td>320593836</td>\n",
       "      <td>pv</td>\n",
       "      <td>6562</td>\n",
       "      <td>2014-12-06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>98145908</td>\n",
       "      <td>290208520</td>\n",
       "      <td>pv</td>\n",
       "      <td>13926</td>\n",
       "      <td>2014-12-16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99996</th>\n",
       "      <td>38839327</td>\n",
       "      <td>142636078</td>\n",
       "      <td>pv</td>\n",
       "      <td>5503</td>\n",
       "      <td>2014-12-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99997</th>\n",
       "      <td>86239261</td>\n",
       "      <td>389384457</td>\n",
       "      <td>pv</td>\n",
       "      <td>3662</td>\n",
       "      <td>2014-12-06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99998</th>\n",
       "      <td>57301300</td>\n",
       "      <td>255239748</td>\n",
       "      <td>pv</td>\n",
       "      <td>11770</td>\n",
       "      <td>2014-12-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99999</th>\n",
       "      <td>2935703</td>\n",
       "      <td>329752390</td>\n",
       "      <td>pv</td>\n",
       "      <td>3673</td>\n",
       "      <td>2014-11-22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>100000</th>\n",
       "      <td>11083802</td>\n",
       "      <td>339361946</td>\n",
       "      <td>pv</td>\n",
       "      <td>10761</td>\n",
       "      <td>2014-11-25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100001 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            用户ID       商品ID 行为类型   商品类别        登录时间\n",
       "0       98047837  232431562   pv   4245  2014-12-06\n",
       "1       97726136  383583590   pv   5894  2014-12-09\n",
       "2       98607707   64749712   pv   2883  2014-12-18\n",
       "3       98662432  320593836   pv   6562  2014-12-06\n",
       "4       98145908  290208520   pv  13926  2014-12-16\n",
       "...          ...        ...  ...    ...         ...\n",
       "99996   38839327  142636078   pv   5503  2014-12-01\n",
       "99997   86239261  389384457   pv   3662  2014-12-06\n",
       "99998   57301300  255239748   pv  11770  2014-12-10\n",
       "99999    2935703  329752390   pv   3673  2014-11-22\n",
       "100000  11083802  339361946   pv  10761  2014-11-25\n",
       "\n",
       "[100001 rows x 5 columns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看行为类型列总共分为几种值\n",
    "taobaoData1['行为类型'].unique()\n",
    "#替换\n",
    "taobaoDataList=list(taobaoData1['行为类型'])   #找到原表这一列转化为list\n",
    "for i in taobaoDataList:\n",
    "    if 1== i:\n",
    "        taobaoData1['行为类型']=taobaoData1['行为类型'].replace(i,'pv')\n",
    "    elif 2== i:\n",
    "        taobaoData1['行为类型']=taobaoData1['行为类型'].replace(i,'fav')\n",
    "    elif 3== i:\n",
    "        taobaoData1['行为类型']=taobaoData1['行为类型'].replace(i,'cart')\n",
    "    elif 4== i:\n",
    "        taobaoData1['行为类型']=taobaoData1['行为类型'].replace(i,'buy')\n",
    "#查看数据\n",
    "taobaoData1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bfc60d54-0a11-4247-b195-23b8b1d9e243",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "行为类型\n",
       "buy       952\n",
       "cart     2652\n",
       "fav      1853\n",
       "pv      94544\n",
       "Name: 用户ID, dtype: int64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#行为类型量\n",
    "behaviorTypeNum1=taobaoData1.groupby('行为类型')['用户ID'].count()\n",
    "behaviorTypeNum1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2d00f879-7bbc-4882-8afd-2b8aea468a94",
   "metadata": {},
   "outputs": [],
   "source": [
    "#点击量\n",
    "behaviorPV=behaviorTypeNum1['pv']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "13c72317-16af-41c1-8f50-c2422420bfeb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用户ID的唯一值： 899\n",
      "商品ID的唯一值： 80962\n",
      "行为类型的唯一值： 4\n",
      "商品类别的唯一值： 3811\n",
      "登录时间的唯一值： 31\n"
     ]
    }
   ],
   "source": [
    "# 查看每列的唯一值\n",
    "def uniqueValue(data):\n",
    "    for column in data.columns:\n",
    "# format 格式化函数\n",
    "        print('{}的唯一值：'.format(column),len(data[column].unique()))\n",
    "uniqueValue(taobaoData1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "85b85c17-4472-4df1-848f-2648ab3214c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "登录时间\n",
      "2014-11-18    440\n",
      "2014-11-19    414\n",
      "2014-11-20    442\n",
      "2014-11-21    447\n",
      "2014-11-22    431\n",
      "2014-11-23    452\n",
      "2014-11-24    446\n",
      "2014-11-25    451\n",
      "2014-11-26    430\n",
      "2014-11-27    436\n",
      "2014-11-28    442\n",
      "2014-11-29    459\n",
      "2014-11-30    458\n",
      "2014-12-01    462\n",
      "2014-12-02    464\n",
      "2014-12-03    457\n",
      "2014-12-04    452\n",
      "2014-12-05    448\n",
      "2014-12-06    470\n",
      "2014-12-07    474\n",
      "2014-12-08    482\n",
      "2014-12-09    478\n",
      "2014-12-10    490\n",
      "2014-12-11    519\n",
      "2014-12-12    618\n",
      "2014-12-13    467\n",
      "2014-12-14    461\n",
      "2014-12-15    472\n",
      "2014-12-16    485\n",
      "2014-12-17    475\n",
      "2014-12-18    446\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "#一天内各个时刻的访客数\n",
    "uniqueUserHour=taobaoData1.groupby('登录时间')['用户ID'].value_counts()\n",
    "uniqueUserHour1=uniqueUserHour.groupby('登录时间').count()\n",
    "print(uniqueUserHour1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "a6a985e1-8df6-43bb-856c-08a09bd7f418",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "登录时间\n",
       "2014-11-18    440\n",
       "2014-11-19    414\n",
       "2014-11-20    442\n",
       "2014-11-21    447\n",
       "2014-11-22    431\n",
       "2014-11-23    452\n",
       "2014-11-24    446\n",
       "2014-11-25    451\n",
       "2014-11-26    430\n",
       "2014-11-27    436\n",
       "2014-11-28    442\n",
       "2014-11-29    459\n",
       "2014-11-30    458\n",
       "2014-12-01    462\n",
       "2014-12-02    464\n",
       "2014-12-03    457\n",
       "2014-12-04    452\n",
       "2014-12-05    448\n",
       "2014-12-06    470\n",
       "2014-12-07    474\n",
       "2014-12-08    482\n",
       "2014-12-09    478\n",
       "2014-12-10    490\n",
       "2014-12-11    519\n",
       "2014-12-12    618\n",
       "2014-12-13    467\n",
       "2014-12-14    461\n",
       "2014-12-15    472\n",
       "2014-12-16    485\n",
       "2014-12-17    475\n",
       "2014-12-18    446\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#每天的用户访问数\n",
    "uniqueUV=taobaoData1.groupby('登录时间')['用户ID'].value_counts()\n",
    "uniqueUV1=uniqueUV.groupby('登录时间').count()\n",
    "uniqueUV1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "4c47b6c3-3a82-4768-bb1b-514ed72c448c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "行为类型\n",
       "buy       952\n",
       "cart     2652\n",
       "fav      1853\n",
       "pv      94544\n",
       "Name: 用户ID, dtype: int64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#行为类型数量\n",
    "behaviorTypeNum1=taobaoData1.groupby('行为类型')['用户ID'].count()\n",
    "#查看不同的行为类型的数量\n",
    "behaviorTypeNum1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "4537a4d2-0b48-4232-854c-148642eaf274",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "收藏加购转化率: 4.76%\n",
      "购买转化率: 21.13%\n"
     ]
    }
   ],
   "source": [
    "# 点击量\n",
    "behaviorPV = behaviorTypeNum1['pv']\n",
    "# 收藏量\n",
    "behaviorFAV = behaviorTypeNum1['fav']\n",
    "# 加购量\n",
    "behaviorCart = behaviorTypeNum1['cart']\n",
    "# 购买量\n",
    "behaviorBuy = behaviorTypeNum1['buy']\n",
    "\n",
    "# 计算转化率\n",
    "# 由于收藏和加购没有先后顺序，所以二者合并后计算转化率——收藏加购转化率\n",
    "\n",
    "# 收藏加购量总和\n",
    "behaviorFCSum = behaviorFAV + behaviorCart\n",
    "\n",
    "# 收藏加购转化率 (收藏加购/点击量)\n",
    "behaviorFCRatio = behaviorFCSum / behaviorPV * 100\n",
    "\n",
    "# 购买转化率 (购买/收藏加购量总和)\n",
    "behaviorBuyRatio = behaviorBuy / behaviorFCSum * 100\n",
    "\n",
    "# 显示转化率\n",
    "print(f\"收藏加购转化率: {behaviorFCRatio:.2f}%\")  # 显示到小数点后两位\n",
    "print(f\"购买转化率: {behaviorBuyRatio:.2f}%\")  # 显示到小数点后两位\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f0c6e230-35a8-42e2-b3aa-43b111b89742",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "148      101260672\n",
       "152      116730636\n",
       "158      104811265\n",
       "185      106230218\n",
       "207      100684618\n",
       "           ...    \n",
       "99701     57301300\n",
       "99713     35389479\n",
       "99722     35389479\n",
       "99803     35389479\n",
       "99880     86239261\n",
       "Name: 用户ID, Length: 952, dtype: object"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##购买用户数及用户的复购率\n",
    "# 提取行为类型为‘buy’的用户ID\n",
    "#进行分组\n",
    "behaviorTypeUserNum=taobaoData1.groupby('行为类型')['用户ID']\n",
    "#将groupby对象转换为list\n",
    "behaviorTypeUserList=list(behaviorTypeUserNum)\n",
    "#提取‘buy’行为类型对应的用户ID\n",
    "behaviorTypeUserNumList1=behaviorTypeUserList[0][1]\n",
    "behaviorTypeUserNumList1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "df05c289-aef4-4039-a113-73bc374a1da9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>用户ID</th>\n",
       "      <th>购买次数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>101260672</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>116730636</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>104811265</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>106230218</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>100684618</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>421</th>\n",
       "      <td>39095072</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>422</th>\n",
       "      <td>53451276</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>423</th>\n",
       "      <td>57301300</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>424</th>\n",
       "      <td>35389479</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>425</th>\n",
       "      <td>54388253</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>426 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          用户ID  购买次数\n",
       "0    101260672     1\n",
       "1    116730636     4\n",
       "2    104811265     1\n",
       "3    106230218     3\n",
       "4    100684618     3\n",
       "..         ...   ...\n",
       "421   39095072     2\n",
       "422   53451276     1\n",
       "423   57301300     2\n",
       "424   35389479     9\n",
       "425   54388253     1\n",
       "\n",
       "[426 rows x 2 columns]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import Counter\n",
    "#对列表中的元素进行计数\n",
    "userBuyNum=Counter(behaviorTypeUserNumList1)\n",
    "#转换为字典\n",
    "userBuyNumDict=dict(userBuyNum)\n",
    "#字典转换成数据框\n",
    "userBuyNumFrame=pd.DataFrame.from_dict(userBuyNumDict,orient='index',columns=['购买次数'])\n",
    "userBuyNumFrame=userBuyNumFrame.reset_index().rename(columns={'index':'用户ID'})\n",
    "userBuyNumFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "06683116-25ba-4aea-865d-e4d35fa95e56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "426"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#统计具有购买行为的用户的数量\n",
    "userBuyNumFrame['用户ID'].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "b036b46c-90de-4503-ac42-ed8e371ba679",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "227"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求取购买次数大于2次的用户数\n",
    "buyNum=userBuyNumFrame['购买次数']\n",
    "count=0\n",
    "for num in buyNum:\n",
    "    if num>=2:\n",
    "        count=count+1\n",
    "count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "74b83d3f-77e6-4f0e-9a52-06caf44daf28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "复购率: 25.00%\n"
     ]
    }
   ],
   "source": [
    "# 假设定义了复购用户数和总购买用户数\n",
    "count = 500  # 复购用户数\n",
    "userBuyNumSum = 2000  # 总购买用户数\n",
    "\n",
    "# 计算复购率\n",
    "repurchaseRate = count / userBuyNumSum * 100\n",
    "\n",
    "# 输出复购率\n",
    "print(f\"复购率: {repurchaseRate:.2f}%\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "c8cce62b-fa95-43c1-9c94-405e9100b915",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RF-score为110的用户数量: 227\n",
      "RF-score为120的用户数量: 16\n",
      "RF-score为210的用户数量: 115\n",
      "RF-score为310的用户数量: 62\n"
     ]
    }
   ],
   "source": [
    "import datetime\n",
    "import pandas as pd\n",
    "import math \n",
    "behaviorTypeUserNum = taobaoData1.groupby('行为类型')['用户ID']\n",
    "behaviorTypeUserList = list(behaviorTypeUserNum)\n",
    "behaviorTypeUserNumTuple = behaviorTypeUserList[0][1]  # 'buy' 对应的用户ID\n",
    "behaviorTypeUserStr = behaviorTypeUserList[0][0]  # 'buy' 字符串\n",
    "\n",
    "# 提取出行为类型为‘buy’的数据框\n",
    "userBehaviourList = []\n",
    "for i in behaviorTypeUserNumTuple:\n",
    "    userBehaviour1 = dict(list(taobaoData1.groupby(['用户ID', '行为类型'])))[(i, behaviorTypeUserStr)]\n",
    "    userBehaviourFrame1 = pd.DataFrame(userBehaviour1)\n",
    "    userBehaviourList.append(userBehaviourFrame1)\n",
    "userBehaviourFrameCon = pd.concat(userBehaviourList)\n",
    "\n",
    "# 求取各个顾客最近购买行为的距离天数的函数\n",
    "def countDays(maxDateGroupList, maxDate):\n",
    "    timeList = []\n",
    "    maxDateTime = datetime.datetime.strptime(maxDate, \"%Y-%m-%d\")\n",
    "    for group in maxDateGroupList:\n",
    "        groupTime = datetime.datetime.strptime(group, \"%Y-%m-%d\")\n",
    "        timeRDay = (maxDateTime - groupTime).days\n",
    "        timeList.append(timeRDay)\n",
    "    return timeList\n",
    "\n",
    "# 获取所有用户最近购买日期\n",
    "maxDateGroup = userBehaviourFrameCon.groupby('用户ID')['登录时间'].max()\n",
    "maxDateGroupList = list(maxDateGroup)\n",
    "\n",
    "# 获取数据集中的最大日期，假设是今天\n",
    "maxDate = datetime.datetime.now().strftime(\"%Y-%m-%d\")\n",
    "\n",
    "# 求取各个用户最近购买日期距离数据采集时的天数——即R值\n",
    "dateList = countDays(maxDateGroupList, maxDate)\n",
    "\n",
    "# 购买次数计数——即F值\n",
    "buyNumCount = userBehaviourFrameCon.groupby('用户ID')['行为类型'].count()\n",
    "buyNumCountList = list(buyNumCount)\n",
    "\n",
    "# 求取R、F的最大、最小、三等分距\n",
    "maxR = max(dateList)\n",
    "minR = min(dateList)\n",
    "trisectionDistanceR = (maxR - minR) / 3\n",
    "\n",
    "maxF = max(buyNumCountList)\n",
    "minF = min(buyNumCountList)\n",
    "trisectionDistanceF = (maxF - minF) / 3\n",
    "\n",
    "# 计算R-score、F-score\n",
    "# 计算R-score\n",
    "scoreRList = []\n",
    "for i in dateList:\n",
    "    if math.ceil((i - minR) / trisectionDistanceR) == 0:\n",
    "        scoreRList.append(1)\n",
    "    else:\n",
    "        scoreRList.append(math.ceil((i - minR) / trisectionDistanceR))\n",
    "\n",
    "# 计算F-score\n",
    "scoreFList = []\n",
    "for i in buyNumCountList:\n",
    "    if math.ceil((i - minF) / trisectionDistanceF) == 0:\n",
    "        scoreFList.append(1)\n",
    "    else:\n",
    "        scoreFList.append(math.ceil((i - minF) / trisectionDistanceF))\n",
    "\n",
    "# 计算 RF-score = 100*R-score + 10*F-score + 1*M-score\n",
    "# 由于数据集没有涉及消费金额，暂时不考虑M-score\n",
    "RF_scoreList = []\n",
    "for index in range(len(scoreRList)):\n",
    "    RF_scoreList.append(100 * scoreRList[index] + 10 * scoreFList[index])\n",
    "\n",
    "# 统计不同的RF-score出现的次数\n",
    "RF_scoreList110 = RF_scoreList.count(110)\n",
    "RF_scoreList120 = RF_scoreList.count(120)\n",
    "RF_scoreList210 = RF_scoreList.count(210)\n",
    "RF_scoreList310 = RF_scoreList.count(310)\n",
    "\n",
    "# 输出统计结果\n",
    "print(f\"RF-score为110的用户数量: {RF_scoreList110}\")\n",
    "print(f\"RF-score为120的用户数量: {RF_scoreList120}\")\n",
    "print(f\"RF-score为210的用户数量: {RF_scoreList210}\")\n",
    "print(f\"RF-score为310的用户数量: {RF_scoreList310}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15d79b79-b73d-49a2-900f-3d0bc1355b7a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
